<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-inline">
                    <input name="marks" lay-verify="required" lay-reqtext="备注不能为空" placeholder="请输入备注" value=""
                        class="layui-input">
                </div>
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;

            //监听提交
            form.on('submit(saveBtn)', function (data) {

                let parent_selectDevices = JSON.parse(parent.g_selectDevices)

                let updateDevices = {}

                let alreadySaveDevices = []

                for (let i = 0; i < parent_selectDevices.length; i++) {
                    let selectInfo = parent_selectDevices[i]
                    // 判断是否为账号
                    if (selectInfo.parentId === -1) {
                        continue;
                    }
                    let newData = {
                        deviceName: selectInfo.deviceName,
                        accountNickName: selectInfo.accountNickName,
                        accountId: selectInfo.accountId,
                        gatherTitleNum: selectInfo.gatherTitleNum,
                        accountType: selectInfo.accountType,
                        accountRemarks: data.field.marks,
                        accountStatus: selectInfo.accountStatus
                    }
                    // 判断设备是否已经出现过
                    let deviceName = selectInfo.parentId
                    // 如果出现过则直接将账号信息添加在已经有的设备中, 否则创建一个新的字段并添加
                    if (deviceName in alreadySaveDevices) {
                        updateDevices[deviceName].push(newData)
                    } else {
                        updateDevices[deviceName] = []
                        updateDevices[deviceName].push(newData)
                    }
                }

                let postData = {
                    updateDevices: updateDevices
                }

                $.ajax({
                    url: '/api/gather/updateDeviceAccounts', // API 地址
                    method: 'POST', // 请求方法
                    contentType: 'application/json', // 请求内容类型
                    data: JSON.stringify(postData), // 将数据转换为 JSON 字符串
                    success: function (response) {
                        // 处理成功的响应
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        parent.renderTable()
                    },
                    error: function (xhr, status, error) {
                        // 处理错误响应
                        console.error('编辑类目失败:', xhr.responseJSON);
                        // 可以在这里显示错误提示
                        layer.msg(`编辑类目失败，${xhr.responseJSON.message}`);
                    }
                });

                return false;
            });

        });
    </script>
</body>

</html>